package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.font.QuestionsAnswersTo;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.QuestionsAnswers;
import com.jeff.tianti.cms.dto.QuestionsAnswersQueryDTO;
import org.apache.commons.lang3.StringUtils;

/**
 * @author xujianfang
 * @desc QuestionsAnswersDaoImpl��
 * @date 2019-07-04
 */

public class QuestionsAnswersDaoImpl extends CustomBaseSqlDaoImpl implements QuestionsAnswersDaoCustom {

    public PageModel<QuestionsAnswers> queryQuestionsAnswersPage(QuestionsAnswersQueryDTO questionsAnswersQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from QuestionsAnswers t where 1 = 1 ");
        if (questionsAnswersQueryDTO != null) {
            String state = questionsAnswersQueryDTO.getState();
            if (StringUtils.isNotBlank(state) && !"-1".equals(state)) {
                hql.append(" and t.state = :state ");
                map.put("state", state);
            }
            String auditFlag = questionsAnswersQueryDTO.getAuditFlag();
            if (StringUtils.isNotBlank(auditFlag) && !"-1".equals(auditFlag)) {
                hql.append(" and t.auditFlag = :auditFlag ");
                map.put("auditFlag", auditFlag);
            }
        }
        hql.append(" order by t.createDate desc ");

        return this.queryForPageWithParams(hql.toString(), map, questionsAnswersQueryDTO.getCurrentPage(), questionsAnswersQueryDTO.getPageSize());
    }

    public List<QuestionsAnswers> queryQuestionsAnswersList(QuestionsAnswersQueryDTO questionsAnswersQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from QuestionsAnswers t ");
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public PageModel<QuestionsAnswers> queryQuestionsAnswersPages(QuestionsAnswersQueryDTO dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from QuestionsAnswers t where t.auditFlag = 2 and t.deleteFlag = 0 ");
        return this.queryForPageWithParams(hql.toString(), map, dto.getCurrentPage(), dto.getPageSize());
    }

    @Override
    public PageModel<QuestionsAnswers> findAllByTo(QuestionsAnswersTo questionsAnswersTo) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from QuestionsAnswers t where t.deleteFlag = 0 ");
        if (questionsAnswersTo != null) {
            String auditFlag = questionsAnswersTo.getAuditFlag();
            if (StringUtils.isNotBlank(auditFlag)) {
                hql.append(" and t.auditFlag = :auditFlag ");
                map.put("auditFlag", auditFlag);
            }
            String state = questionsAnswersTo.getState();
            if (StringUtils.isNotBlank(state)) {
                hql.append(" and t.state = :state ");
                map.put("state", state);
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, questionsAnswersTo.getCurrentPage()+1, questionsAnswersTo.getPageSize());
    }


}